package ar.edu.unlu.sistemas.p2p.business.strategy;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import ar.edu.unlu.sistemas.p2p.business.util.Params;
import ar.edu.unlu.sistemas.p2p.business.util.QueryResultComparator;
import ar.edu.unlu.sistemas.p2p.business.vo.QueryResultVO;

public abstract class ProcessQueryStrategy {

	public abstract Collection<QueryResultVO> execute(String query);

	protected Collection<QueryResultVO> filterResults(Collection<QueryResultVO> queryResults) {
		
		Collection<QueryResultVO> outResults = new ArrayList<QueryResultVO>();
		//Ordenamos los resultados con un umbral flotante
		Collections.sort((List<QueryResultVO>)queryResults, new QueryResultComparator());
		Iterator<QueryResultVO> sortedResults = queryResults.iterator();
		if (sortedResults.hasNext()) {
			Float ac1 = new Float(0);
			QueryResultVO qrTempVO = sortedResults.next();
			while ((qrTempVO.getValue() > Params.THRESHOLD*ac1) && sortedResults.hasNext()) {
				outResults.add(qrTempVO);
				ac1 = (Float) qrTempVO.getValue();
				qrTempVO = sortedResults.next();
			}
			if ((qrTempVO.getValue() > Params.THRESHOLD*ac1)) {
				outResults.add(qrTempVO);
			}
		}
		return outResults;
	}
}
